//
//  CustomMessageCell.m
//  IceBreaker
//
//  Created by 老焦 on 12-10-20.
//  Copyright (c) 2012年 NDS. All rights reserved.
//

#import "CustomMessageCell.h"
#import "QuartzCore/CALayer.h"

#define SELF_WIDTH 320.0f
#define GAP_WIDTH 7.0f
#define VIEW_WIDTH 306.0f
#define HEAD_IMG_SIDE 50.0f
#define ICON_SIDE 12.0f

@implementation CustomMessageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id) initWithHeadImg: (UIImage *)headIcon
                 name: (NSString *)name
             distance: (float)distance
             sendTime: (NSDate *)time
             msgState: (int) stateCode
           msgContent: (NSString *)content
      reuseIdentifier: (NSString *)reuseIdentifier {
    self = [super initWithStyle: UITableViewCellStyleDefault
                reuseIdentifier: reuseIdentifier];
    if (self) {
        UIView *cellView = [[UIView alloc] initWithFrame: CGRectMake(GAP_WIDTH, GAP_WIDTH, VIEW_WIDTH, 95)];
        cellView.layer.shadowColor = [UIColor purpleColor].CGColor;
        cellView.layer.shadowOffset = CGSizeMake(0, 1);
        cellView.layer.shadowOpacity = 1;
        cellView.layer.shadowRadius = 3.0;
        cellView.clipsToBounds = NO;
        
        UIImageView *headImg = [[UIImageView alloc] initWithFrame: CGRectMake(GAP_WIDTH, GAP_WIDTH, HEAD_IMG_SIDE, HEAD_IMG_SIDE)];
        headImg.image = headIcon;
        headImg.layer.masksToBounds = YES;
        headImg.layer.cornerRadius = 5.0;
        [cellView addSubview: headImg];
        [headImg release];

        UILabel *nameLabel = [[UILabel alloc] initWithFrame: CGRectMake(62, GAP_WIDTH, 240, 30)];
        nameLabel.font = [UIFont systemFontOfSize: 12];
        nameLabel.numberOfLines = 0;
        nameLabel.text = name;
        [cellView addSubview: nameLabel];
        [nameLabel release];
        
        UIFont *font = [UIFont systemFontOfSize: 10.0f];
        UILabel *distanceLabel = [[UILabel alloc] initWithFrame: CGRectMake(62, 45, 48, ICON_SIDE)];
        distanceLabel.font = font;
        distanceLabel.textAlignment = UITextAlignmentLeft;
        distanceLabel.text = [NSString stringWithFormat: @"%0.1fkm", distance];
        [cellView addSubview: distanceLabel];
        [distanceLabel release];
        
        UILabel *dateLabel = [[UILabel alloc] initWithFrame: CGRectMake(112, 45, 140, ICON_SIDE)];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
        dateLabel.font = font;
        dateLabel.textAlignment = UITextAlignmentCenter;
        dateLabel.text = [formatter stringFromDate: time];
        [cellView addSubview: dateLabel];
        [dateLabel release];
        
        UILabel *stateLabel = [[UILabel alloc] initWithFrame: CGRectMake(254, 45, 48, ICON_SIDE)];
        stateLabel.font = font;
        stateLabel.textAlignment = UITextAlignmentRight;
        if (stateCode == 1) {
            stateLabel.text = @"已送达";
        } else {
            stateLabel.text = @"失败";
        }
        [cellView addSubview: stateLabel];
        [stateLabel release];
        [formatter release];
        
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(GAP_WIDTH, 62, 292, 30)];
        contentLabel.frame = CGRectMake(GAP_WIDTH, 62, 292, 30);
        contentLabel.font = [UIFont systemFontOfSize: 15.0f];
        contentLabel.numberOfLines = 1;
        contentLabel.text = content;
        [cellView addSubview: contentLabel];
        [contentLabel release];
        
        [cellView setBackgroundColor: [UIColor whiteColor]];
        [self addSubview: cellView];
        self.frame = CGRectMake(0, 0, SELF_WIDTH, 105);
        
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
